﻿using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace Synthetic.IVS.Plugins.Custom
{
    public partial class PluginForm : Form
    {
        public PluginForm()
        {
            InitializeComponent();
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextPane.Text = "";
        }

        private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            Close();
        }

        [STAThread]
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "Save file..";
            sfd.RestoreDirectory = true;
            sfd.InitialDirectory = @"C:\";
            sfd.Filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*";
            sfd.SupportMultiDottedExtensions = true;
            DialogResult d = sfd.ShowDialog();
            if (d == DialogResult.OK)
            {
                FileStream fs = new FileStream(sfd.FileName, FileMode.OpenOrCreate);
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.Write(TextPane.Text);
                }
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            lc.Text = TextPane.Lines.Count().ToString();
            cc.Text = TextPane.Text.Count().ToString();
        }

        private void PluginForm_Load(object sender, EventArgs e)
        {
            this.Text = "Simple Text Editor - Inside: " + Process.GetCurrentProcess().ProcessName;
        }
        
        [STAThread]
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Open file..";
            ofd.InitialDirectory = @"C:\";
            ofd.Filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*";
            ofd.Multiselect = false;
            DialogResult d = ofd.ShowDialog();
            if (d == DialogResult.OK)
            {
                FileStream fs = new FileStream(ofd.FileName, FileMode.OpenOrCreate);
                using (StreamReader sr = new StreamReader(fs))
                {
                    TextPane.Text = sr.ReadToEnd();
                }
            }
        }

        private void disableWordWrapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (WordWrapToggle.Text == "Disable Word Wrap")
            {
                TextPane.WordWrap = false;
                WordWrapToggle.Text = "Enable Word Wrap";
            }
            else
            {
                TextPane.WordWrap = true;
                WordWrapToggle.Text = "Disable Word Wrap";
            }
        }

        private void selectColourToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            DialogResult d = color.ShowDialog();
            if (d == System.Windows.Forms.DialogResult.OK)
            {
                TextPane.ForeColor = color.Color;
            }
        }
    }
}
